package com.zjc.yohoshop.mvp.view.fragment;


import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.CommunityEntity;
import com.zjc.yohoshop.di.compont.DaggerCommunityFragmentCompont;
import com.zjc.yohoshop.di.modules.CommunityFragmentModules;
import com.zjc.yohoshop.listener.HomeBannerImageLoader;
import com.zjc.yohoshop.mvp.contract.CommunityFragmentContract;
import com.zjc.yohoshop.mvp.persenter.CommunityPresenter;
import com.zjc.yohoshop.mvp.view.adapter.CommunityAdaptrr;
import com.zjc.yohoshop.mvp.view.adapter.CommunityViewPagerAdapter;
import com.zjc.yohoshop.network.API;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区视图
 */
public class CommunityFragment extends BaseFragment<CommunityPresenter> implements CommunityFragmentContract.CommunityFragmentIView, BaseRefreshListener {
    private boolean banner_0 = true;
    private Banner banner;
    private ViewPager viewPager;
    private RecyclerView rv_community;
    private PullToRefreshLayout ptl_community;
    private List<CommunityEntity.ValuesBean> list = new ArrayList<>();
    private CommunityAdaptrr adapter;

    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_community, null);
        return view;
    }

    @Override
    public void init(View view) {
        ptl_community = view.findViewById(R.id.ptl_community);

        //最新头布局控件初始化
        View view_head = View.inflate(getActivity(), R.layout.item_community_head, null);
        banner = view_head.findViewById(R.id.ban_item_community_head);
        viewPager = view_head.findViewById(R.id.vp_item_community_head);
        viewPager.setOffscreenPageLimit(3);//一页显示3条
        viewPager.setPageMargin(20);//每页外边距
        //列表控件初始化
        rv_community = view.findViewById(R.id.rv_community);
        rv_community.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new CommunityAdaptrr(list);

        adapter.addHeaderView(view_head, 0);
        rv_community.setAdapter(adapter);
    }

    @Override
    public void initData() {
        p.request();
    }

    @Override
    public void setupDagger() {
        DaggerCommunityFragmentCompont.builder().communityFragmentModules(new CommunityFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        ptl_community.setRefreshListener(this);
        //数据点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), ((CommunityAdaptrr)adapter).getData().get(position).title, Toast.LENGTH_SHORT).show();
            }
        });
        //banner点击事件
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), "" + (position - 1), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void success(CommunityEntity entity) {
        if (entity.values == null || entity.values.size() == 0){
            Toast.makeText(getActivity(), "没数据了", Toast.LENGTH_SHORT).show();
            return;
        }

        for (int i = 0; i < entity.values.size(); i++){
            if (i % 2 == 0){
                entity.values.get(i).itemType = CommunityEntity.Community0;
            }else{
                entity.values.get(i).itemType = CommunityEntity.Community1;
            }
        }
        adapter.addData(entity.values);

        if (banner_0) {
            List<String> strs = new ArrayList<>();
            strs.add(API.BaseUrl + "/recommend_img/recommend1.jpg");
            strs.add(API.BaseUrl + "/recommend_img/recommend1.jpg");
            strs.add(API.BaseUrl + "/recommend_img/recommend1.jpg");
            banner.setImages(strs);
            banner.setImageLoader(new HomeBannerImageLoader());
            banner.isAutoPlay(true);
            banner.setIndicatorGravity(BannerConfig.CENTER);
            banner.setDelayTime(1 * 3000);
            banner.start();

            List<String> imgs = new ArrayList<>();
            imgs.add("/recommend_img/recommend1.jpg");
            imgs.add("/recommend_img/recommend2.jpg");
            imgs.add("/recommend_img/recommend3.jpg");
            imgs.add("/recommend_img/recommend4.jpg");
            imgs.add("/recommend_img/recommend5.jpg");
            viewPager.setAdapter(new CommunityViewPagerAdapter(imgs));
            viewPager.setCurrentItem(imgs.size() / 2);

            banner_0 = !banner_0;
        }


    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        adapter.getData().clear();
        p.refresh();
        ptl_community.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.loadMore();
        ptl_community.finishLoadMore();
    }
}
